package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpFragment;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.cases.views.impl.CaseActivity;
import com.sanjiahantang.youjiarole.module.common.adapters.HomeAdapter;
import com.sanjiahantang.youjiarole.module.common.presenters.HomePresenter;
import com.sanjiahantang.youjiarole.module.common.views.HomeView;
import com.sanjiahantang.youjiarole.module.join.views.impl.CertificationActivity;
import com.sanjiahantang.youjiarole.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiarole.utils.CheckUtil;


/**
 * Created by Administrator on 2017/6/7.
 */

public class HomeFragment extends BaseMvpFragment<HomeView,HomePresenter> implements HomeView,OnClickListener,SwipeRefreshLayout.OnRefreshListener {

    private View rootView,vsNetworkError;
    private RecyclerView mRecyclerView;
    private ImageView scan;
    private SwipeRefreshLayout refreshLayout;
    private LinearLayoutManager layoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
        }
        return rootView;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        presenter.onActivityResult(requestCode, resultCode, data);
    }

    public Dialog getDialogBindDesigner(boolean flag,String message) {
        if (dialogBindDesigner == null){
            dialogBindDesigner = new Dialog(getContext(),R.style.dialog_common);
            dialogBindDesigner.setContentView(R.layout.dialog_designer_bind_result);
            dialogBindDesigner.findViewById(R.id.btnOK).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialogBindDesigner.dismiss();
                }
            });
        }
        TextView title = (TextView) dialogBindDesigner.findViewById(R.id.title);
        TextView txtHint = (TextView) dialogBindDesigner.findViewById(R.id.txtHint);
        ImageView ivResult = (ImageView) dialogBindDesigner.findViewById(R.id.ivResult);
        if(flag){
            title.setText("绑定成功");
            txtHint.setText(message);
            ivResult.setImageResource(R.drawable.i_success);
        }else{
            title.setText("绑定失败");
            txtHint.setText(message);
            ivResult.setImageResource(R.drawable.i_failure);
        }
        return dialogBindDesigner;
    }

    private Dialog dialogBindDesigner;

    @Override
    public HomePresenter initPresenter() {
        return new HomePresenter();
    }

    @Override
    protected void initView() {

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        scan = (ImageView) rootView.findViewById(R.id.ivScanning);
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refreshLayout);
        vsNetworkError = rootView.findViewById(R.id.vsNetworkError);
        presenter.init(context);

        rootView.findViewById(R.id.ivMessage).setOnClickListener(this);
        scan.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
        layoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    @Override
    public void onRefresh() {
        presenter.onLayoutRefresh(context);
    }


    @Override
    protected void initData() {


    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.loadData(context);
        mRecyclerView.addOnScrollListener(presenter.getScrollerListener());
    }

    @Override
    public void onPause() {
        super.onPause();
        mRecyclerView.removeOnScrollListener(presenter.getScrollerListener());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivScanning:
                if (!TextUtils.isEmpty(ConfigValue.DATA_KEY)){
                    CheckUtil.isRoleValid(context, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            presenter.openScanner(context,HomeFragment.this);
                        }
                    });
                }else{
                    LoginActivity.logout(context);
                }

                break;
            case R.id.ivMessage:
                startActivity(new Intent(context,MessageActivity.class));
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        presenter.onRequestPermissionsResult(context,requestCode,permissions,grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    public AMapLocationClient getAMapLocationClient() {
        return new AMapLocationClient(context);
    }


    @Override
    public void setLayoutRefreshing(boolean b) {
        refreshLayout.setRefreshing(b);
    }

    @Override
    public void initRecyclerView(HomeAdapter adapter) {
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
    }


    @Override
    public void getMoreHomePage() {
        presenter.getMoreHomePage(context);
    }

    @Override
    public void showToast(String s) {
        Utils.showToast(context,s);
    }

    @Override
    public void showProgress() {
        refreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void showDialog(boolean flag,String message) {
        getDialogBindDesigner(flag,message).show();
    }

    @Override
    public void onNetError(boolean b) {
        if (b){
            refreshLayout.setVisibility(View.GONE);
            vsNetworkError.setVisibility(View.VISIBLE);
            vsNetworkError.findViewById(R.id.btnReloadNetwork).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRefresh();
                }
            });
        }else{
            refreshLayout.setVisibility(View.VISIBLE);
            vsNetworkError.setVisibility(View.GONE);
        }
    }
}
